<template>
  <div class="apps-dialog-overlay" @click.self="handleClose">
    <div class="apps-dialog-container">
      <div class="dialog-header">
        <h2>
          <i class="el-icon-menu"></i>
          全部应用
          <span class="sub-title">共{{ filteredApps.length }}个应用</span>
        </h2>
        <div class="search-box">
          <el-input
            v-model="searchQuery"
            placeholder="搜索应用..."
            prefix-icon="el-icon-search"
            clearable
          />
        </div>
        <el-button 
          icon="el-icon-close" 
          circle 
          plain
          @click="handleClose"
        ></el-button>
      </div>
      
      <div class="dialog-body">
        <div class="apps-grid">
          <div 
            v-for="app in filteredApps" 
            :key="app.id"
            class="app-item"
            @click="handleAppClick(app)"
          >
            <div class="app-icon">
              <i :class="app.icon"></i>
            </div>
            <div class="app-name">{{ app.name }}</div>
          </div>
        </div>
      </div>
      
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    apps: {
      type: Array,
      default: () => []
    },
    show: Boolean
  },
  data() {
    return {
      searchQuery: '',
      selectedApp: null
    }
  },
  computed: {
    filteredApps() {
      return this.apps.filter(app => 
        app.name.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
        app.keywords?.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  },
  methods: {
    handleAppClick(app) {
      this.selectedApp = app
      this.$emit('select', app)
      this.handleClose()
    },
    handleClose() {
      this.$emit('update:show', false)
    }
  }
}
</script>

<style scoped>
.apps-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 2000;
  display: flex;
  justify-content: center;
  align-items: center;
  animation: fadeIn 0.3s;
}

.apps-dialog-container {
  width: 90%;
  max-width: 900px;
  height: 80vh;
  background: white;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.dialog-header {
  padding: 16px 24px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #e8e8e8;
  background: #f7f7f7;
}

.dialog-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  align-items: center;
  flex: 1;
}

.dialog-header h2 i {
  margin-right: 8px;
  color: #409EFF;
}

.sub-title {
  margin-left: 12px;
  font-size: 14px;
  color: #666;
  font-weight: normal;
}

.search-box {
  flex: 1;
  max-width: 300px;
  margin: 0 20px;
}

.dialog-body {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
}

.apps-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 16px;
}

.app-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.app-item:hover {
  background-color: #f5f5f5;
  transform: translateY(-2px);
}

.app-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background-color: #409EFF;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  margin-bottom: 8px;
}

.app-name {
  font-size: 13px;
  text-align: center;
  word-break: break-all;
}

.dialog-footer {
  padding: 12px 24px;
  border-top: 1px solid #e8e8e8;
  text-align: right;
}

@media (max-width: 768px) {
  .apps-grid {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  }
  
  .app-icon {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}
</style>